package tourist.springboot.web;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import tourist.springboot.bean.ResponseData;
import tourist.springboot.domain.Foods;
import tourist.springboot.service.FoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import tourist.springboot.utils.Constants;
import tourist.springboot.utils.IDGenerateUtils;
import tourist.springboot.utils.ResponseDataUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author wangzg
 * 美食控制器
 */
@RestController
public class FoodsController {

    @Autowired
    private FoodsService foodsService;

    /**
     * 获取美食列表
     * @return
     */
    @ApiOperation(value = "查询美食列表", notes = "查询美食列表")
    @GetMapping("/foods")
    public ResponseData foodsList(){
        return ResponseDataUtils.successResponse(foodsService.findAll());
    }

    /**
     * 查看美食详细信息
     * @param id
     * @return
     */
    @GetMapping("/foods/{id}")
    public ResponseData getFoodsById(@PathVariable("id") String id){
        if(StringUtils.isNotEmpty(id)){
            return ResponseDataUtils.successResponse(foodsService.findById(id));
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 新增美食信息
     * @param foods
     * @return
     */
    @PostMapping("/foods")
    public ResponseData addFoods(@RequestBody Foods foods){
        if(null != foods){
            foods.setId(IDGenerateUtils.generateId());
            foodsService.insert(foods);
            return ResponseDataUtils.successResponse();
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 修改美食信息
     * @param foods
     * @return
     */
    @PutMapping("/foods")
    public ResponseData updateFoods(@RequestBody Foods foods){
        if(null != foods){
            foodsService.update(foods);
            return ResponseDataUtils.successResponse();
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 删除美食
     * @param id
     * @return
     */
    @DeleteMapping("/foods/{id}")
    public ResponseData deleteFoods(@PathVariable("id") String id){
        if(StringUtils.isNotEmpty(id)){
            foodsService.delete(id);
            return ResponseDataUtils.successResponse();
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 获取美食相关推荐
     * @param id
     * @return
     */
    @GetMapping("/foods/{id}/recommend")
    public ResponseData getRecommendListByFoods(@PathVariable("id") String id){
        if(StringUtils.isNotEmpty(id)){
            Foods foods = foodsService.findById(id);
            if(null != foods){
                String recommendIdStr = foods.getRecommend();
                if(StringUtils.isNotEmpty(recommendIdStr)){
                    List<Foods> recommendList = foodsService.findFoods(Arrays.asList(recommendIdStr.split(",")));
                    return ResponseDataUtils.successResponse(recommendList);
                }
            }

        }
        return ResponseDataUtils.errorResponse();
    }
}
